
package dynamicdiscountsystem;

import com.mysql.jdbc.PreparedStatement;
import java.io.IOException;
import java.sql.SQLException;
import java.text.DateFormat;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;


public class Discount2Frame extends javax.swing.JInternalFrame {

    /** Creates new form Discount2Frame */
    public Discount2Frame() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jTextFieldDiscountPercent = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jCalendarComboFrom = new org.freixas.jcalendar.JCalendarCombo();
        jLabel3 = new javax.swing.JLabel();
        jCalendarComboTo = new org.freixas.jcalendar.JCalendarCombo();
        jLabel4 = new javax.swing.JLabel();
        jTextFieldLimitPrice = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();

        setTitle("Məbləğ Endirimi");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Endirim Məlumatları"));

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 13)); // NOI18N
        jLabel1.setText("alış - veriş edən şəxsə həmin mal üçün");

        jTextFieldDiscountPercent.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jTextFieldDiscountPercent.setText("20.5");

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 13)); // NOI18N
        jLabel2.setText("- tarixindən");

        jCalendarComboFrom.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "June 14, 2011" }));
        jCalendarComboFrom.setDateFormat(DateFormat.getDateInstance(DateFormat.LONG,
            getLocale()));

    jLabel3.setFont(new java.awt.Font("Tahoma", 0, 13)); // NOI18N
    jLabel3.setText("% endirim.");

    jCalendarComboTo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "June 14, 2011" }));
    jCalendarComboTo.setDateFormat(DateFormat.getDateInstance(DateFormat.LONG,
        getLocale()));

jLabel4.setFont(new java.awt.Font("Tahoma", 0, 13)); // NOI18N
jLabel4.setText("- tarixinədək eyni maldan qiyməti");

jTextFieldLimitPrice.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
jTextFieldLimitPrice.setText("100.5");

jLabel5.setFont(new java.awt.Font("Tahoma", 0, 13)); // NOI18N
jLabel5.setText("AZN - dən artıq");

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
    jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(jPanel1Layout.createSequentialGroup()
        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextFieldDiscountPercent, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jCalendarComboFrom, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addGap(18, 18, 18)
                .addComponent(jCalendarComboTo, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jTextFieldLimitPrice, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(4, 4, 4)
                .addComponent(jLabel5)))
        .addGap(59, 59, 59))
    );
    jPanel1Layout.setVerticalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel1Layout.createSequentialGroup()
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jCalendarComboFrom, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jLabel2)
                .addComponent(jCalendarComboTo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jLabel4)
                .addComponent(jTextFieldLimitPrice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jLabel5))
            .addGap(18, 18, 18)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel1)
                .addComponent(jTextFieldDiscountPercent, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jLabel3))
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );

    jButton1.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
    jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/dynamicdiscountsystem/resources/ok.gif"))); // NOI18N
    jButton1.setText("Əlavə et");
    jButton1.setHideActionText(true);
    jButton1.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
    jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
    jButton1.setInheritsPopupMenu(true);
    jButton1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton1ActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                .addComponent(jButton1)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 753, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addContainerGap(40, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(jButton1)
            .addContainerGap(33, Short.MAX_VALUE))
    );

    pack();
    }// </editor-fold>//GEN-END:initComponents


    private void ActionPerfomed() throws SQLException, IOException {
        java.sql.Date dateFrom = new java.sql.Date(jCalendarComboFrom.getDate().getTime());
        java.sql.Date dateTo = new java.sql.Date(jCalendarComboTo.getDate().getTime());
        float limitPrice = Float.valueOf(jTextFieldLimitPrice.getText());
        float discountPercent = Float.valueOf(jTextFieldDiscountPercent.getText());

        DatabaseConnection db = new DatabaseConnection();
        PreparedStatement ps = null;
        try {
            db.ConnectionOpen();
            ps = (PreparedStatement) db.conn.prepareStatement("INSERT INTO DISCOUNT2 (DATE_FROM,DATE_TO,"
                    + "LIMIT_PRICE,DISCOUNT_PERCENT) VALUES(?,?,?,?)");
            ps.setDate(1, dateFrom);
            ps.setDate(2, dateTo);
            ps.setFloat(3, limitPrice);
            ps.setFloat(4, discountPercent);
            ps.execute();
            JOptionPane.showMessageDialog(null,
                    "Məlumat əlavə edildi.",
                    "İnformasiya", JOptionPane.INFORMATION_MESSAGE);
        } finally {
            if (ps != null) {
                ps.close();
            }
            db.ConnectionClose();
        }
    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        try {
            ActionPerfomed();
        } catch (Exception ex) {
             JOptionPane.showMessageDialog(null,
                    "Xəta baş verdi.\nXəta mesajı = " + ex.getMessage(),
                    "Xəta", JOptionPane.ERROR_MESSAGE);
        }
       
    }//GEN-LAST:event_jButton1ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private org.freixas.jcalendar.JCalendarCombo jCalendarComboFrom;
    private org.freixas.jcalendar.JCalendarCombo jCalendarComboTo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField jTextFieldDiscountPercent;
    private javax.swing.JTextField jTextFieldLimitPrice;
    // End of variables declaration//GEN-END:variables

}
